package robotter.bluecambot;



import robotter.bluecambot.communication;
import robotter.bluecambot.game_element;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.util.Log;

public class traitementimg {
	public List<game_element> detected_elem = new ArrayList<game_element>();
	
	public Bitmap image=null;
	public Bitmap seuil_img=null;
	private int[] raw_data;
	
	private native int JavaInterface(int[] raw_data, int height, int width);
	
	public void compute_position(Bitmap pimage, communication comm){
		Log.d("traitement_img","Ready to compute");
		
		image=pimage;
		raw_data=new int[image.getWidth()*image.getHeight()];
		image.getPixels(raw_data, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
		
		int prg_ret=JavaInterface(raw_data, image.getHeight(), image.getWidth());
		//int prg_ret=432432;
		Log.d("traitement_img","Le programme en C a retourne " + String.valueOf(prg_ret));
		Log.d("traitement_img","Le programme en C a trouve " + String.valueOf(detected_elem.size()) +" elements");

		if (seuil_img!=null){
			int i;
			for (game_element dzone: detected_elem){
				if (dzone.color!=configuration.ip_seuil_img) continue;
				for (i=dzone.pos_x;i<dzone.pos_x+dzone.width;i++){
					seuil_img.setPixel(i, dzone.pos_y, 0xff00ff00);
					seuil_img.setPixel(i, dzone.pos_y+dzone.heigth-1, 0xff00ff00);
				}
				for (i=dzone.pos_y;i<dzone.pos_y+dzone.heigth;i++){
					seuil_img.setPixel(dzone.pos_x,i, 0xff00ff00);
					seuil_img.setPixel(dzone.pos_x+dzone.width-1,i, 0xff00ff00);
				}
			}
		}
		
		
		//comm.send_data(detected_elem);
		detected_elem.clear();
		Log.d("traitement_img","Computation done");		
	}
	
	@SuppressWarnings("unused")
	private void add_to_list(int seuil, int xmin, int xmax, int ymin, int ymax){
		game_element elem1 = new game_element();
		elem1.color=seuil;
		elem1.heigth=ymax-ymin;
		elem1.width=xmax-xmin;
		elem1.pos_x=xmin;
		elem1.pos_y=ymin;
		detected_elem.add(elem1);
	}
	
	@SuppressWarnings("unused")
	private int init_seuil_export(int width, int height){
		if (seuil_img==null) seuil_img=Bitmap.createBitmap(width, height,image.getConfig());
		return configuration.ip_seuil_img;
	}
	
	@SuppressWarnings("unused")
	private void set_seuil_export(int width, int height, int value){
		seuil_img.setPixel(width, height, value);
	}
		
	@SuppressWarnings("unused")
	private int get_config_param_threshold(int threshold, int fieldid){
		//fieldid:
		// 0: actif
		// 1: mode
		// 2: H
		// 3: Y
		// 4: or_avec
		// 5: and_avec
		// 6: utilise_zone
		// 7: Zone: Largeur minimum
		// 8: Zone: Hauteur minimum
		// 9: Zone: Largeur maximum
		// 10: Zone: Hauteur maximum
		// 11: Recherche de zone X minimum
		// 12: Recherche de zone Y minimum
		// 13: Recherche de zone X maximum
		// 14: Recherche de zone Y maximum
		switch(threshold){
			case 0:
				return configuration.ip_t_0[fieldid];
			case 1:
				return configuration.ip_t_1[fieldid];
			case 2:
				return configuration.ip_t_2[fieldid];
			case 3:
				return configuration.ip_t_3[fieldid];
			case 4:
				return configuration.ip_t_4[fieldid];
			case 5:
				return configuration.ip_t_5[fieldid];
			case 6:
				return configuration.ip_t_6[fieldid];
			case 7:
				return configuration.ip_t_7[fieldid];
			case 8:
				return configuration.ip_t_8[fieldid];
			case 9:
				return configuration.ip_t_9[fieldid];
			case 10:
				return configuration.ip_t_10[fieldid];
			case 11:
				return configuration.ip_t_11[fieldid];
			case 12:
				return configuration.ip_t_12[fieldid];
			case 13:
				return configuration.ip_t_13[fieldid];
			case 14:
				return configuration.ip_t_14[fieldid];
			case 15:
				return configuration.ip_t_15[fieldid];
			default:
				return 0;
		}
	}
	
    static {
        System.loadLibrary("BlueCamBot_v1");
    }
	
}

